package edu.colorado.karl.databases;

import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/**
 * Commits set point changes to WebCTRL.
 */
public class ChangeSetpoint {

	private ChangeSetpoint()
	{
		//empty
	}
	/**
	 * Updates set points in WebCTRL
	 * @param setpointName Name of set point which needs to be changed,
	 * @param value The new value.
	 */
	public static void updateSetpoint(String setpointName, int value)
	{	
		StringBuilder cookie = new StringBuilder();
		try {
			// Construct data
			String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode("administrator", "UTF-8");
			data += "&" + URLEncoder.encode("pwd", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8");

			// Send data
			URL url = new URL("http://karl.colorado.edu:81/_common/servlet/wap/login?t=X");
			URLConnection conn = url.openConnection();
			conn.setDoOutput(true);
			OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
			wr.write(data);
			wr.flush();
			wr.close();

			System.out.println("DATA: " + data);

			for (int i=0; ; i++) {
				String headerName = conn.getHeaderFieldKey(i);
				String headerValue = conn.getHeaderField(i);

				//System.out.println("headerName = " + headerName + "\n");
				//System.out.println("headerValue = " + headerValue + "\n");

				if (headerName == null && headerValue == null) {
					// No more headers
					break;
				}
				//System.out.println("!!!!!!!!!!!!");
				if ("Set-Cookie".equalsIgnoreCase(headerName)) {
					//System.out.println("------------");
					// Parse cookie
					String[] fields = headerValue.split(";\\s*");

					String cookieValue = fields[0];
					//System.out.println("cookieValue = " + cookieValue);

					/*for(int a = 0; a < fields.length; a++){
	                	System.out.println("FIELD = " + fields[a]);
	                }*/

					if(cookie.length()==0)
						cookie.append(cookieValue);
					else
						cookie.append("; "+cookieValue);
				}
			}

			//url = new URL("http://karl.colorado.edu:81/~relpath/m004?as=action-set&oldlocation=3");
			url = new URL("http://karl.colorado.edu:81/~relpath/m128?as=action-set&oldlocation=5");
			conn = url.openConnection();
			//conn.setDoOutput(true);
			//System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			conn.setRequestProperty("Cookie", cookie.toString());
			//System.out.println("COOKIE_VALUE = " + cookie.toString());

			//conn.setDoOutput(true);

			//conn.connect();

			url = new URL("http://karl.colorado.edu:81/_common/servlet/wap/setvalue?editid=prim_106_ctrlid1");
			conn = url.openConnection();
			conn.setRequestProperty("Cookie", cookie.toString());
			String content = "value=" + URLEncoder.encode("10.0", "UTF-8");
			conn.setDoOutput(true);
			wr = new OutputStreamWriter(conn.getOutputStream());
			wr.write(content);

			conn.connect();

			// Get the response
			/*BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	        String line;
	        while ((line = rd.readLine()) != null) {
	            System.out.println(line);
	        }
	        rd.close();*/




		} catch (Exception e) {
			System.out.println(e.getLocalizedMessage());
		}
	}
}

